/*
 * generate by table-builder.Don't modify it
 */
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay;
using System.Collections.Generic;


namespace CfgTable
{
    public partial class TbCfgFormula
    {
        private readonly Dictionary<int, CfgFormula> _dataMap;
        private readonly List<CfgFormula> _dataList;


        public TbCfgFormula(ByteBuf _buf)
        {
            //first read common data
            _TbCommonFormula _commonData = null;
            var commonSize = _buf.ReadSize();
            if (commonSize > 0)
            {
                _commonData = new _TbCommonFormula(_buf);
            }

            //then read row data
            var size = _buf.ReadSize();
            _dataMap = new Dictionary<int, CfgFormula>(size * 3 / 2);
            _dataList = new List<CfgFormula>(size);

            for (int i = 0; i < size; i++)
            {
                CfgFormula _v;
                _v = CfgFormula.DeserializeCfgFormula(_buf, _commonData);
                _dataList.Add(_v);
                _dataMap.Add(_v.Id, _v);
            }

            // read extra fields 

            PostInit();
        }

        public IReadOnlyDictionary<int, CfgFormula> DataMap => _dataMap;
        public IReadOnlyList<CfgFormula> DataList => _dataList;

        public CfgFormula Get(int key)
        {
            if (_dataMap.TryGetValue(key, out var v))
            {
                return v;
            }
            IQIGame.Onigao.Framework.LogGame.LogError("[TbCfgFormula] config not found,id:" + key.ToString());

            return null;
        }

        public CfgFormula GetWithoutError(int key) => _dataMap.TryGetValue(key, out var v) ? v : null;

        /// <summary>
        /// table data file
        /// </summary>
        public static string TableFileName { get; } = "formula";

        /// <summary>
        /// load table function
        /// </summary>
        public static TbCfgFormula LoadFromFunc(System.Func<string, ByteBuf> loadFunc)
        {
            return new TbCfgFormula(loadFunc(TableFileName));
        }

        /// <summary>
        /// post process table
        /// </summary>
        partial void PostInit();

    }

    public partial class CfgFormula
    {
        private CfgFormula(ByteBuf _buf, _TbCommonFormula _commonData)
        {
            Id = _buf.ReadInt();
            { int dataIndex = _buf.ReadInt() - 1; Value = _commonData._field0[dataIndex]; }

            PostInit();
        }

        internal static CfgFormula DeserializeCfgFormula(ByteBuf _buf, _TbCommonFormula _commonData)
        {
            return new CfgFormula(_buf, _commonData);
        }

        /// <summary>
        /// major key
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// 开启效果
        /// </summary>
        public string Value { get; private set; }


        /// <summary>
        /// post process table
        /// </summary>
        partial void PostInit();
    }

    /// <summary>
    /// internal common data.Optimize memory
    /// </summary>
    internal class _TbCommonFormula
    {

        internal IReadOnlyList<string> _field0 { get; private set; }
        internal _TbCommonFormula(ByteBuf _buf)
        {
            { int __n0 = _buf.ReadSize(); var __f0 = new string[__n0]; _field0 = __f0; for (var __i0 = 0 ; __i0 < __n0 ; __i0++ ){ string __v0; __v0 = _buf.ReadString(); __f0[__i0] = __v0;} }
        }

    }
}
